package com.yuexunit.finance.member.action.v1;

import java.math.BigDecimal;
import java.util.Date;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.member.action.v1.dto.MemberGradeCastor;
import com.yuexunit.finance.member.action.v1.model.MemberGrade;
import com.yuexunit.finance.member.action.v1.model.MemberGradeDetail;
import com.yuexunit.finance.member.action.v1.model.MemberGradeUpdateParams;
import com.yuexunit.finance.member.action.v1.model.SupplierCommissionGradeDetail;
import com.yuexunit.finance.member.entity.EnterpriseEntity;
import com.yuexunit.finance.member.entity.MemberGradeEntity;
import com.yuexunit.finance.member.entity.SupplierCommissionGradeEntity;
import com.yuexunit.finance.member.service.EnterpriseService;
import com.yuexunit.finance.member.service.MemberGradeService;
import com.yuexunit.finance.member.service.SupplierCommissionGradeService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.annotation.Transactional;

/**
 * @ClassName: MemberGradeApiImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年9月23日 下午5:54:25
 */
@Implements(MemberGradeActionApi.class)
public class MemberGradeApiImpl implements MemberGradeActionApi {

	private MemberGradeService memberGradeService = ComponentFactory.loadClass(MemberGradeService.class);
	
	private SupplierCommissionGradeService supplierCommissionGradeService = ComponentFactory.loadClass(SupplierCommissionGradeService.class);
	
	private EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);

	@Transactional
	@Override
	public String createMemberGrade(MemberGrade params) {
		MemberGradeEntity entity = MemberGradeCastor.fromMemberGrade(params);
		memberGradeService.createMemberGrade(entity);
		return entity.getMemberGradeId().toString();
	}

	@Transactional
	@Override
	public void updateMemberGrade(MemberGradeUpdateParams params) {
		MemberGradeEntity entity = MemberGradeCastor.fromMemberGrade(params);
		entity.setMemberGradeId(params.getMemberGradeId());
		memberGradeService.updateMemberGrade(entity);
	}

	@Override
	public void disableMemberGrade(Long memberGradeId) {
		memberGradeService.disableMemberGrade(memberGradeId);
	}

	@Override
	public MemberGradeDetail inquireMemberGradeDetail(Long memberGradeId) {
		MemberGradeEntity entity = memberGradeService.inquireMemberGradeDetail(memberGradeId);
		return MemberGradeCastor.toMemberGradeDetail(entity);
	}

	@Override
	public PageList<MemberGradeDetail> inquireMemberGradePageList(PageCriteria page) {
		PageList<MemberGradeEntity> pages = memberGradeService.inquireMemberGradeList(page);
		PageList<MemberGradeDetail> list = new PageList<>(page, pages.getTotalRecordCount());
		for (MemberGradeEntity entity : pages) {
			list.add(MemberGradeCastor.toMemberGradeDetail(entity));
		}
		return list;
	}

	@Override
	public PageList<MemberGradeDetail> inquireMemberGradeByGradeName(PageCriteria page, String gradeName) {
		PageList<MemberGradeEntity> pages = memberGradeService.inquireMemberGradeByGradeName(page, gradeName);
		PageList<MemberGradeDetail> list = new PageList<>(page, pages.getTotalRecordCount());
		for (MemberGradeEntity entity : pages) {
			list.add(MemberGradeCastor.toMemberGradeDetail(entity));
		}
		return list;
	}

	@Override
	public String createSupplierCommissionGrade(Long enterpriseId, String commissionGradeName, String expenseName, BigDecimal annualRateStart,
			BigDecimal annualRateEnd, BigDecimal expenseRate, Date effectiveDateStart, Date effectiveDateEnd) {
		SupplierCommissionGradeEntity entity = new SupplierCommissionGradeEntity();
		entity.setCommissionId(enterpriseId);
		if (enterpriseId == 0) {
			entity.setCommissionName("所有");
		} else {
			EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(enterpriseId);
			entity.setCommissionName(enterprise.getEnterpriseName());
		}
		entity.setCommissionGradeName(commissionGradeName);
		entity.setExpenseName(expenseName);
		entity.setAnnualRateEnd(annualRateEnd);
		entity.setAnnualRateStart(annualRateStart);
		entity.setExpenseRate(expenseRate);
		entity.setEffectiveDateEnd(effectiveDateEnd);
		entity.setEffectiveDateStart(effectiveDateStart);
		supplierCommissionGradeService.createSupplierCommissionGrade(entity);
		return entity.getCommissionGradeId().toString();
	}

	@Override
	public SupplierCommissionGradeDetail inquireSupplierCommissionGradeDetail(Long commissionGradeId) {
		SupplierCommissionGradeEntity entity = supplierCommissionGradeService.inquireSupplierCommissionGrade(commissionGradeId);
		SupplierCommissionGradeDetail detail = new SupplierCommissionGradeDetail();
		BeanUtils.copyProperties(entity, detail);
		return detail;
	}

	@Override
	public PageList<SupplierCommissionGradeDetail> inquireSupplierCommissionGradePageList(PageCriteria page) {
		PageList<SupplierCommissionGradeEntity> pages = supplierCommissionGradeService.inquireSupplierCommissionGradePageList(page);
		PageList<SupplierCommissionGradeDetail> results = new PageList<>(page, pages.getTotalRecordCount());
		for (SupplierCommissionGradeEntity scge : pages) {
			SupplierCommissionGradeDetail detail = new SupplierCommissionGradeDetail();
			BeanUtils.copyProperties(scge, detail);
			results.add(detail);
		}
		return results;
	}
}
